<?php

use com\gdl\newagk\interfaces\AdminloginServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\newagk\dataobject\adminlogin\AdminloginDTO;
use com\gdl\newagk\dataobject\adminlogin\AdminloginRequestDTO;


class api_AdminloginService extends api_Abstract implements AdminloginServiceIf
{

	//管理员登录
    public function login(AdminloginRequestDTO $login)
    {
		$result = new ResultDO();
		$username = gdl_lib_BaseUtils::getStr($login->username);
		$password = gdl_lib_BaseUtils::getStr($login->password);
		try{	
			$adminuser = new model_lhbase_appadminuser();
			$user = $adminuser->selectOne(" (idcard='".$username."' or account='".$username."') and isDelete=1",'id userid,platformId,passsalt,realName,password,userType,idcard,account,adminReg,status,unitsId');
	
			$result->code = 0;
			$result->success = true;
		
			if(empty($user['account'])){
				$result->message = '该账号暂未注册，请核对用户信息或联系平台管理员';
				return $result;
			}
			
			if($user['status']!=1){
				$result->message = '该账号已被禁用';
				return $result;
			}
	
			/**验证密码***/
			if(parent::dpassword($password , $user['passsalt'])!=$user['password']){
				$result->message = '身份证号与密码不匹配，注意区分大小写';
				return $result;
			}
			
			/**验证是否有权限***/
			if($user['userid']=='1' && $user['account']=='admin'){
				
				$platform_status = ['reals'=>'总管理员'];
				
			} else {
				/*$adminuser = new model_lhbase_adminroleuser();
			
				$role = $adminuser->selectOne(['userId'=>$user['userid']],'roleId');
				$db_access = new model_lhbase_adminuseraccess();
				$adminuseraccess = $db_access->selectOne(array('userId' => $user['userid']),'node_id');
				if(empty($role['role_id']) && empty($adminuseraccess['node_id'])){
					//$result->message = '该账号暂无管理权限，请核对用户信息或联系平台管理员';
					//return $result;
				}*/
				/**验证平台是否关闭***/
				$platform = new model_lhbase_platform();
				$platform_status = $platform->getInfoById($user['platformId'],'status,isDelete,platformName');
		
				if($platform_status['status']!='1' || $platform_status['isDelete']>0){
					$result->message = '平台已关闭，请联系平台管理员';
					return $result;
				}
			}
			$authuserunits = new model_lhbase_authuserunits();
			$unitId = $authuserunits->selectOne(['adminUserId'=>$user['userid']],'unitsId');
			$result->data[] = [
								'name'=>$user['account'],
								'adminUserId'=>$user['userid'],
								'realName'=>$user['realName'],
								'adminReg'=>$user['adminReg'],
								'platformId'=>$user['platformId'],
								'platformName'=>$platform_status['platformName'],
								'unitId'=>empty($unitId['unitsId']) ? 0 : $unitId['unitsId'],
								];

         
            $result->code = 1;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
	
	//修改管理员密码
	public function edit(AdminloginRequestDTO $login)
    {
		$result = new ResultDO();
		$adminUserId = $login->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($login->filename['adminUserId']) : 0;
		$password =  $login->filename['password'] ? $login->filename['password'] : '';
		$oldPassWord =  $login->filename['oldPassWord'] ? $login->filename['oldPassWord'] : '';
		
		if(!$adminUserId || empty($password) || empty($oldPassWord)){
				$result->message = '缺少参数';
				return $result;
			}

		try{	
			$adminuser = new model_lhbase_appadminuser();
			$user = $adminuser->selectOne("id=".$adminUserId,'id userid,passsalt,password,status,account');
			
			$result->code = 0;
			$result->success = true;
		
			if(empty($user['account'])){
				$result->message = '该账号暂未注册，请核对';
				return $result;
			}

			/**验证密码***/
			if(parent::dpassword($oldPassWord , $user['passsalt'])!=$user['password']){
				$result->message = '原始密码不匹配，注意区分大小写';
				return $result;
			}
			$data = [
					'password'=>parent::dpassword($password ,'0'),
					'passsalt'=>'0',
					];
		$userid = $adminuser->update(['id'=>$adminUserId],$data);
		$result->message = '修改成功';
		$result->code = 1;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

}
